package mystructs;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import vo.Person;

import com.opensymphony.xwork2.ActionSupport;

import dao.PersonDao;

public class PersonAction extends ActionSupport {
		Person vo;
		int index;
	public int getIndex() {
			return index;
		}

		public void setIndex(int index) {
			this.index = index;
		}

	public String addform() throws Exception {
		this.LOG.info("PersonAction.addfrom");
		return "addform";
	}

	public String add() throws Exception {
		this.LOG.info("PersonAction.add");
		this.LOG.info("vo.getUser:" + vo.getUser());
		PersonDao dao = new PersonDao();
		dao.insert(vo);
		java.util.ArrayList<Person> list = shopcar();
		list.add(vo);
		return "add";
	}

	public String list() throws Exception {
		this.LOG.info("person.list");
		// this.setRsList(this.shopcar());
		return "list";
	}
	public String updateform() throws Exception {
		this.LOG.info("person.updateform");
		this.setVo(shopcar().get(this.index));
		
		return "updateform";
	}
	public String update() throws Exception {
		this.LOG.info("PersonAction.update");
		//this.LOG.info("vo.getUser:" + vo.getUser());
		//PersonDao dao = new PersonDao();
		//dao.insert(vo);
		java.util.ArrayList<Person> list = shopcar();
		list.set(index, vo);
		return "update";
	}
	public String delete() throws Exception {
		this.LOG.info("PersonAction.delete");
		//this.LOG.info("vo.getUser:" + vo.getUser());
		//PersonDao dao = new PersonDao();
		//dao.insert(vo);
		java.util.ArrayList<Person> list = shopcar();
		list.remove(index);
		return list();
	}


	java.util.ArrayList<Person> shopcar() {
		// java.util.ArrayList<Person> list=new java.util.ArrayList<Person>();
		HttpSession session = ServletActionContext.getRequest().getSession();
		java.util.ArrayList<Person> list = (java.util.ArrayList<Person>) session
				.getAttribute("list");
		if (list == null) {
			list = new java.util.ArrayList<Person>();
			session.setAttribute("list", list);
		}

		return list;
	}

	

	public Person getVo() {
		return vo;
	}

	public void setVo(Person vo) {
		this.vo = vo;
	}

	/*
	 * public java.util.ArrayList<Person> getRsList() { return rsList; } public
	 * void setRsList(java.util.ArrayList<Person> rsList) { this.rsList =
	 * rsList; } java.util.ArrayList<Person> rsList;
	 */
	public java.util.ArrayList<Person> getRsList() {
		return shopcar();
	}

}
